/**
 * Nettle terminal list menu code
 * (C) Nettle developers 2000-2001
 *
 * $Id: termlist,v 1.5 2001/09/06 14:52:21 archifishal Exp $
 */

#include "generic.h"
#include "globals.h"

#include "messages.h"
#include "nettle.h"
#include "termlist.h"
#include "wimp.h"

#include <ctype.h>


static int items = -1;
static int *menublock = NULL;


void
lose_termlist_menu (void)
{
  if (menublock)
    free (menublock);
  menublock = NULL;
}


const int *
create_termlist_menu (void)
{
  struct session_struct *node;
  int position = 7;
  int termitems = 0;
  int wide=100;

  if (!sessions)
    return NULL;

  node = sessions;
  while (node)
  {
    termitems++;
    node = node->next;
  }

  if (termitems != items)
  {
    free(menublock);
    items = termitems;
    menublock = (int *) malloc(24 * items + 28);
    if (!menublock)
      return NULL;
  }

  lookup("Session", (char *)menublock, 12);

  menublock[3] = 0x70207; /* standard menu colours */
  menublock[4] = wide;
  menublock[5] = 44;
  menublock[6] = 0;

  node = sessions;
  while (node)
  {
    menublock[position++] = node->next ? 0 : 0x80;
      /* set "last" bit if this is the end of the list */
    menublock[position++] = -1;	/* no submenu */
    menublock[position++] =
      WIMP_ICON_FGCOL (7) | WIMP_ICON_VCENT_BIT | WIMP_ICON_FILLED_BIT |
      WIMP_ICON_TEXT_BIT | WIMP_ICON_INDIRECTED_BIT | WIMP_ICON_SPRITE_BIT;

    menublock[position++]=(int) node->label;
    menublock[position++]=(int)(node->connection_type == NETTLE_SSH ? "snettle_ssh" : \
                                  node->connection_type == NETTLE_TELNET ? "snettle_telnt" :
                                  "snettle_task");
    menublock[position++]=strlen(node->label)+1;

    if (menublock[position-1]+1 * 16 > wide)
      wide =  menublock[position-1]+1 * 16;

    node = node->next;
  }

  menublock[4] = wide;

  return menublock;
}


struct session_struct *
get_termlist_entry (int entry)
{
  struct session_struct *node = sessions;

  for (; entry; entry--)
    if ((node = node->next) == NULL)
      return NULL;

  return node;
}
